﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd. 
//--------------------------------------------------------------------

using System;
using System.Data;
using System.Windows.Forms;

namespace DotNet.WinForm.LogOn
{
    using DotNet.Model;
    using DotNet.Service;
    using DotNet.Utilities;
    using DotNet.WinForm.Utilities;
    using System.Drawing;

    /// <summary>
    /// FrmIPLimitr.cs
    /// 权限管理-IP登录限制
    ///		
    /// 修改记录
    ///
    ///     2009.10.04 版本：1.0 JiRiGaLa  用户按IP地址限制。
    ///		
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>JiRiGaLa</name>
    ///		<date>2009.10.04</date>
    /// </author> 
    /// </summary>    
    public partial class FrmIPLimitr : BaseForm 
    {
        private DataTable DTParameter = new DataTable(BaseParameterTable.TableName); // 参数数据表

        // 目标用户主键
        private string TargetUserId
        {
            set
            {
                this.ucUser.SelectedId = value;
            }
            get
            {
                return this.ucUser.SelectedId;
            }
        }

        // 目标用户姓名     
        private string TargetFullName
        {
            set
            {
                this.ucUser.SelectedFullName = value;
            }
            get
            {
                return this.ucUser.SelectedFullName;
            }
        }

        public FrmIPLimitr()
        {
            InitializeComponent();
        }

        public FrmIPLimitr(string userId, string fullName) : this()
        {
            this.TargetUserId = userId;
            this.TargetFullName = fullName;
        }

        #region public override void FormOnLoad() 加载窗体
        /// <summary>
        /// 加载窗体
        /// </summary>
        public override void FormOnLoad()
        {
            // 获取数据
            this.DTParameter = ServiceManager.Instance.ParameterService.GetDTByParameter(UserInfo, "IPAddress", UserInfo.Id);
            // 往DataTable中添加选择列
            BaseInterfaceLogic.DataTableAddColumn(this.DTParameter, BaseBusinessLogic.SelectedColumn);
            this.grdParameter.AutoGenerateColumns = false;
            this.DTParameter.DefaultView.Sort = BaseParameterTable.FieldParameterCode;
            this.grdParameter.DataSource = this.DTParameter.DefaultView;
        }
        #endregion

        #region public override void SetControlState() 设置按钮状态
        /// <summary>
        /// 设置按钮状态
        /// </summary>
        public override void SetControlState()
        {
            this.ucUser.AllowNull = false;
            this.ucUser.AllowSelect = false;

            this.btnSelectAll.Enabled = false;
            this.btnInvertSelect.Enabled = false;
            this.btnDelete.Enabled = false;

            if ((this.DTParameter != null) && (this.DTParameter.Rows.Count > 0))
            {
                this.btnSelectAll.Enabled = true;
                this.btnInvertSelect.Enabled = true;
                this.btnDelete.Enabled = true;
            }
        }
        #endregion

        #region public override bool CheckInput() 检查输入的有效性
        /// <summary>
        /// 检查输入的有效性
        /// </summary>
        /// <returns>有效</returns>
        public override bool CheckInput()
        {
            bool returnValue = true;
            this.txtIPAddress.Text = this.txtIPAddress.Text.TrimEnd();
            if (this.txtIPAddress.Text.Trim().Length == 0)
            {
                MessageBox.Show(AppMessage.Format(AppMessage.MSG0007, "IPAddress"), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txtIPAddress.Focus();
                return false;
            }
            return returnValue;
        }
        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                ServiceManager.Instance.ParameterService.SetParameter(UserInfo, "IPAddress", UserInfo.Id, this.txtIPAddress.Text, this.txtMacAddress.Text);
                this.FormOnLoad();

                this.txtMacAddress.Text = string.Empty;
                this.txtIPAddress.Text = string.Empty;
                this.txtIPAddress.Focus();
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataRow dataRow in this.DTParameter.Rows)
            {
                dataRow[BaseBusinessLogic.SelectedColumn] = true.ToString();
            }
        }

        private void btnInvertSelect_Click(object sender, EventArgs e)
        {
            foreach (DataRow dataRow in this.DTParameter.Rows)
            {
                if (dataRow[BaseBusinessLogic.SelectedColumn].ToString() == true.ToString())
                {
                    dataRow[BaseBusinessLogic.SelectedColumn] = false.ToString();
                }
                else
                {
                    dataRow[BaseBusinessLogic.SelectedColumn] = true.ToString();
                }
            }
        }

        #region private string[] GetSelecteIds() 获得已被选中主键数组
        /// <summary>
        /// 获得已被选中主键数组
        /// </summary>
        /// <returns>主键数组</returns>
        private string[] GetSelecteIds()
        {
            return BaseInterfaceLogic.GetSelecteIds(this.DTParameter, BaseParameterTable.FieldId, BaseBusinessLogic.SelectedColumn);
        }
        #endregion

        #region private bool CheckInputBatchDelete() 检查输入的有效性
        /// <summary>
        /// 检查删除选择项的有效性
        /// </summary>
        /// <returns>有效</returns>
        private bool CheckInputBatchDelete()
        {
            bool returnValue = false;
            foreach (DataRow dataRow in this.DTParameter.Rows)
            {
                if (dataRow[BaseBusinessLogic.SelectedColumn].ToString() == true.ToString())
                {
                    // 有记录被选中了
                    returnValue = true;
                }
            }
            if (!returnValue)
            {
                MessageBox.Show(AppMessage.MSG0023, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            return returnValue;
        }
        #endregion

        #region public override int BatchDelete() 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns>影响行数</returns>
        public override int BatchDelete()
        {
            int returnValue = 0;
            returnValue = ServiceManager.Instance.ParameterService.BatchDelete(UserInfo, this.GetSelecteIds());
            // 绑定数据
            this.FormOnLoad();
            return returnValue;
        }
        #endregion

        #region public int Delete() 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public int Delete()
        {
            int returnValue = 0;
            if (this.CheckInputBatchDelete())
            {
                if (MessageBox.Show(AppMessage.MSG0015, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    // 设置鼠标繁忙状态
                    this.Cursor = Cursors.WaitCursor;
                    try
                    {
                        returnValue = this.BatchDelete();
                    }
                    catch (Exception ex)
                    {
                        this.ProcessException(ex);
                    }
                    finally
                    {
                        // 设置鼠标默认状态
                        this.Cursor = Cursors.Default;
                    }
                }
            }
            return returnValue;
        }
        #endregion

        private void btnDelete_Click(object sender, EventArgs e)
        {
            // 删除数据
            this.Delete();
        }

        private void grdParameter_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //定义一个画笔，颜色用行标题的前景色填充
            SolidBrush brush = new SolidBrush(this.grdParameter.RowHeadersDefaultCellStyle.ForeColor);
            //得到当前行的行号
            int rowIndex = e.RowIndex + 1;
            //DataGridView的RowHeadersWidth 为了算中间位置
            int rowHeadersWidth = grdParameter.RowHeadersWidth;
            //根据宽度与显示的字符数计算中间位置
            int rowHeadersX = (rowHeadersWidth - rowIndex.ToString().Length * 6) / 2;
            int rowHeadersY = e.RowBounds.Location.Y + 4;
            e.Graphics.DrawString((rowIndex).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.grdParameter.DefaultCellStyle.Font, brush, rowHeadersX, rowHeadersY);
        }
    }
}
